using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Dialogs/Radio List", 1)]
	public class DialogRadioList : MaterialDialog
	{
		[SerializeField]
		private DialogTitleSection m_TitleSection;

		[SerializeField]
		private DialogButtonSection m_ButtonSection;

		[SerializeField]
		private VerticalScrollLayoutElement m_ListScrollLayoutElement;

		[SerializeField]
		private ToggleGroup m_ToggleGroup;

		private List<DialogRadioOption> m_SelectionItems;

		private int m_SelectedIndex;

		private string[] m_OptionList;

		private Action<int> m_OnAffirmativeButtonClicked;

		[SerializeField]
		private GameObject m_OptionTemplate;

		public DialogTitleSection titleSection
		{
			get
			{
				return m_TitleSection;
			}
			set
			{
				m_TitleSection = value;
			}
		}

		public DialogButtonSection buttonSection
		{
			get
			{
				return m_ButtonSection;
			}
			set
			{
				m_ButtonSection = value;
			}
		}

		public VerticalScrollLayoutElement listScrollLayoutElement
		{
			get
			{
				return m_ListScrollLayoutElement;
			}
			set
			{
				m_ListScrollLayoutElement = value;
			}
		}

		public ToggleGroup toggleGroup
		{
			get
			{
				return m_ToggleGroup;
			}
			set
			{
				m_ToggleGroup = value;
			}
		}

		public List<DialogRadioOption> selectionItems => m_SelectionItems;

		public int selectedIndex => m_SelectedIndex;

		public string[] optionList
		{
			get
			{
				return m_OptionList;
			}
			set
			{
				m_OptionList = value;
			}
		}

		public Action<int> onAffirmativeButtonClicked
		{
			get
			{
				return m_OnAffirmativeButtonClicked;
			}
			set
			{
				m_OnAffirmativeButtonClicked = value;
			}
		}

		private void OnEnable()
		{
			GetComponentInChildren<OverscrollConfig>().Setup();
		}

		public void Initialize(string[] options, Action<int> onAffirmativeButtonClicked, string affirmativeButtonText, string titleText, ImageData icon, Action onDismissiveButtonClicked, string dismissiveButtonText, int selectedIndexStart)
		{
			m_OptionList = options;
			m_SelectionItems = new List<DialogRadioOption>();
			for (int i = 0; i < m_OptionList.Length; i++)
			{
				m_SelectionItems.Add(CreateSelectionItem(i));
			}
			if (selectedIndexStart < 0)
			{
				selectedIndexStart = 0;
			}
			if (selectedIndexStart >= m_SelectionItems.Count)
			{
				selectedIndexStart = m_SelectionItems.Count - 1;
			}
			m_SelectionItems[selectedIndexStart].itemRadio.toggle.isOn = true;
			m_SelectedIndex = selectedIndexStart;
			UnityEngine.Object.Destroy(m_OptionTemplate);
			m_TitleSection.SetTitle(titleText, icon);
			m_ButtonSection.SetButtons(null, affirmativeButtonText, onDismissiveButtonClicked, dismissiveButtonText);
			m_ButtonSection.SetupButtonLayout(base.rectTransform);
			m_OnAffirmativeButtonClicked = onAffirmativeButtonClicked;
			float num = DialogManager.rectTransform.rect.height;
			LayoutGroup component = m_TitleSection.text.transform.parent.GetComponent<LayoutGroup>();
			if (component.gameObject.activeSelf)
			{
				component.CalculateLayoutInputVertical();
				num -= component.preferredHeight;
			}
			m_ListScrollLayoutElement.maxHeight = num - 98f;
			Initialize();
		}

		private DialogRadioOption CreateSelectionItem(int i)
		{
			DialogRadioOption component = UnityEngine.Object.Instantiate(m_OptionTemplate).GetComponent<DialogRadioOption>();
			component.rectTransform.SetParent(m_OptionTemplate.transform.parent);
			component.rectTransform.localScale = Vector3.one;
			component.rectTransform.localEulerAngles = Vector3.zero;
			RectTransform rectTransform = component.rectTransform;
			Vector3 localPosition = component.rectTransform.localPosition;
			float x = localPosition.x;
			Vector3 localPosition2 = component.rectTransform.localPosition;
			rectTransform.localPosition = new Vector3(x, localPosition2.y, 0f);
			Text itemText = component.itemText;
			itemText.text = m_OptionList[i];
			component.index = i;
			DialogRadioOption dialogRadioOption = component;
			dialogRadioOption.onClickAction = (Action<int>)Delegate.Combine(dialogRadioOption.onClickAction, new Action<int>(OnItemClick));
			component.itemRadio.toggle.group = m_ToggleGroup;
			component.itemRadio.toggle.isOn = false;
			return component;
		}

		public void OnItemClick(int index)
		{
			Toggle toggle = m_SelectionItems[index].itemRadio.toggle;
			toggle.isOn = !toggle.isOn;
			m_SelectedIndex = index;
		}

		public void AffirmativeButtonClicked()
		{
			m_OnAffirmativeButtonClicked.InvokeIfNotNull(m_SelectedIndex);
			Hide();
		}

		public void DismissiveButtonClicked()
		{
			m_ButtonSection.OnDismissiveButtonClicked();
			Hide();
		}
	}
}
